package CH02链表结构.LeetCode链表;

import CH01数组结构.接口定义.List;

public class LC2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        ListNode rear = head;
        int carry = 0;
        while (true) {
            if (l1 == null && l2 == null && carry == 0) {
                break;
            }
            int num = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
            ListNode n = new ListNode(num % 10);
            rear.next = n;
            rear = n;
            carry = num / 10;
            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;
        }
        return head.next;
    }
}